package com.cartoon.dialog;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.cartoon.R;

import com.cartoon.activity.KKCarttonReadingActivity;
import com.cartoon.activity.LRYCarttonReadingActivity;
import com.cartoon.adapter.KKCartoonDirectoryAdapter;
import com.cartoon.adapter.LRYCartoonDirectoryAdapter;
import com.cartoon.databinding.CartoonDirectoryDialogBinding;
import com.cartoon.model.CartoonDataViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import base.QDialogFragment;
import base.QLoading;
import model.CarttonKKDirectoryModel;
import model.CarttonLRYDirectoryModel;

public class CartoonDirectoryDialog extends QDialogFragment<CartoonDirectoryDialogBinding> {

    private  String lrypic;
    private  List<CarttonKKDirectoryModel.DataBean.TopicInfoBean.ComicsBean> kkList;
    private  List<CarttonLRYDirectoryModel.DataBean.ChapterListBean> lryList;

    private QLoading dialog;
    private boolean sort;
    private List<CarttonKKDirectoryModel.DataBean.TopicInfoBean.ComicsBean> newKKList = new ArrayList<>();


    private List<CarttonLRYDirectoryModel.DataBean.ChapterListBean> newLRYList = new ArrayList<>();
    private LRYCartoonDirectoryAdapter lryCartoonDirectoryAdapter;
    private KKCartoonDirectoryAdapter kkCartoonDirectoryAdapter;
    private String kkname;
    private String lryname;
    public CartoonDirectoryDialog(List<CarttonKKDirectoryModel.DataBean.TopicInfoBean.ComicsBean> comics,String kkname) {
        this.kkList = comics;
        this.kkname =kkname ;
    }

    public CartoonDirectoryDialog(List<CarttonLRYDirectoryModel.DataBean.ChapterListBean> comics ,String pic ,String lryname) {
        this.lryList = comics;
        this.lrypic= pic;
        this.lryname= lryname;
    }
    @Override
    public int getLayoutRes() {
        return R.layout.cartoon_directory_dialog;
    }

    @Override
    public void initView() {
        newKKList.clear();
        newLRYList.clear();
        if (kkList == null && lryList==null) {
            ToastUtils.showLong("没有数据");
        }

        if(lryList!=null){
            for (int i = lryList.size() - 1; i >= 0; i--) {
                newLRYList.add(lryList.get(i));
            }
            binding.rvKkdirectory.setVisibility(View.GONE);
            binding.rvLrydirectory.setVisibility(View.VISIBLE);
        }

        if(kkList!=null){
            for (int i = kkList.size() - 1; i >= 0; i--) {
                newKKList.add(kkList.get(i));
            }
            binding.rvKkdirectory.setVisibility(View.VISIBLE);
            binding.rvLrydirectory.setVisibility(View.GONE);
        }





        LinearLayoutManager kkManager = new LinearLayoutManager(getContext());
        kkManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rvKkdirectory.setLayoutManager(kkManager);
        kkCartoonDirectoryAdapter = new KKCartoonDirectoryAdapter(R.layout.cartton_directory_item, kkList);
        binding.rvKkdirectory.setAdapter(kkCartoonDirectoryAdapter);



        LinearLayoutManager lryManager = new LinearLayoutManager(getContext());
        lryManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rvLrydirectory.setLayoutManager(lryManager);
        lryCartoonDirectoryAdapter = new LRYCartoonDirectoryAdapter(R.layout.cartton_directory_item, lryList);
        lryCartoonDirectoryAdapter.setPic(lrypic);
        binding.rvLrydirectory.setAdapter(lryCartoonDirectoryAdapter);


    }

    @Override
    public void initData() {


    }

    @Override
    public void initListener() {

        binding.tvSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(lryList!=null){
                    LogUtils.d("我点击了"+sort +"--->"+lryList.size()) ;
                    Collections.reverse(lryList);
                    if (sort) {

                        binding.tvSort.setTextColor(Color.parseColor("#000000"));
                        binding.tvSort.setText("正序");
                        sort = false;
                        lryCartoonDirectoryAdapter.replaceData(lryList);
                    } else {
                        sort = true;
                        binding.tvSort.setTextColor(Color.parseColor("#FFC0CB"));
                        binding.tvSort.setText("倒序");
                        lryCartoonDirectoryAdapter.replaceData(lryList);
                    }
                }



                if(kkList!=null){
                    LogUtils.d("我点击了"+sort +"--->"+kkList.size()) ;
                    Collections.reverse(kkList);
                    if (sort) {


                        binding.tvSort.setTextColor(Color.parseColor("#000000"));
                        binding.tvSort.setText("正序");
                        sort = false;
                        kkCartoonDirectoryAdapter.replaceData(kkList);
                    } else {
                        sort = true;
                        binding.tvSort.setTextColor(Color.parseColor("#FFC0CB"));
                        binding.tvSort.setText("倒序");
                        kkCartoonDirectoryAdapter.replaceData(kkList);
                    }
                }


            }

        });
        binding.clBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });



        kkCartoonDirectoryAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                LogUtils.d("我点击了Q");
                CarttonKKDirectoryModel.DataBean.TopicInfoBean.ComicsBean item = (CarttonKKDirectoryModel.DataBean.TopicInfoBean.ComicsBean) adapter.getData().get(position);

                LogUtils.d("我点击了" + item.getTitle());
                Intent intent = new Intent(getActivity(), KKCarttonReadingActivity.class);
                intent.putExtra("kkCartoonId", item.getId());
                intent.putExtra("kkname",kkname);
                List<CarttonKKDirectoryModel.DataBean.TopicInfoBean.ComicsBean> data = kkCartoonDirectoryAdapter.getData();
                CartoonDataViewModel.getInstance().getKKData().postValue(kkCartoonDirectoryAdapter.getData());

                startActivity(intent);//启动intent
            }
        });


        lryCartoonDirectoryAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CarttonLRYDirectoryModel.DataBean.ChapterListBean item = (CarttonLRYDirectoryModel.DataBean.ChapterListBean)adapter.getData().get(position);
//             CarttonReadingActivity
                LogUtils.d("我点击了" + item.getTitle());
                Intent intent = new Intent(getActivity(), LRYCarttonReadingActivity.class);
                intent.putExtra("lrychapterId", item.getChapterId());
                intent.putExtra("lryname",lryname);
                intent.putExtra("lrypic",lrypic);

                List<CarttonLRYDirectoryModel.DataBean.ChapterListBean> data = lryCartoonDirectoryAdapter.getData();
                CartoonDataViewModel.getInstance().getLRYData().postValue(lryCartoonDirectoryAdapter.getData());
                startActivity(intent);
            }
        });
    }

    @Override
    public void networkMessage() {


    }


}
